package com.cynteka.cyfoman.core;

import org.springframework.dao.EmptyResultDataAccessException;
import org.springframework.jdbc.core.JdbcTemplate;

import javax.sql.DataSource;

/**
 * User: ukman
 * Date: 19.07.2011
 * Time: 13:09:43
 */
public class UserNameLookupFunction implements Function<Integer> {
    private DataSource dataSource;
    private String tableName = "employee";
    private String userNameColumn = "username";
    private String idColumn = "id";
    private String name = "username.lookup";

    public String getName() {
        return name;
    }

    @Override
    public Integer getValue() {
        String userName = System.getProperty("user.name");
        JdbcTemplate template = new JdbcTemplate(dataSource);
        try {
            Integer res = template.queryForInt("select id from " + tableName + " where " + userNameColumn + " = ?", userName);
            return res;
        } catch (EmptyResultDataAccessException e) {
            return null;
        }
    }

    public void setName(String name) {
        this.name = name;
    }

    public DataSource getDataSource() {
        return dataSource;
    }

    public void setDataSource(DataSource dataSource) {
        this.dataSource = dataSource;
    }

    public String getTableName() {
        return tableName;
    }

    public void setTableName(String tableName) {
        this.tableName = tableName;
    }

    public String getUserNameColumn() {
        return userNameColumn;
    }

    public void setUserNameColumn(String userNameColumn) {
        this.userNameColumn = userNameColumn;
    }

    public String getIdColumn() {
        return idColumn;
    }

    public void setIdColumn(String idColumn) {
        this.idColumn = idColumn;
    }


}
